﻿using ProjectFootball.MVC.Areas.CompetitionArea.Models;
using ProjectFootball.MVC.Foundation.Repositories;
using ProjectFootball.MVC.Foundation.ResourceLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace ProjectFootball.MVC.Models.EntityFramework.Entities
{
    public static class _CompetitionExtensions
    {
        public static CompetitionDataModel GetDataModel(this Competition competition)
        {
            return new CompetitionDataModel()
            {
                CompetitionGuid = competition.Pk,
                ApprovalGuid = competition.ApprovalGuid,
                CompetitionName = competition.CompetitionName,
                CompetitionType = competition.CompetitionType,
                LocationType = competition.LocationType,
                CountryGuid = competition.CountryGuid,
                ActiveFrom = competition.ActiveFrom,
                ActiveTo = competition.ActiveTo
            };
        }

        public static IEnumerable<CompetitionSearchResultViewModel> Search(this IQueryable<Competition> competitions, IPrincipal user, SearchType searchType)
        {
            var query = competitions.Where(c => !c.IsMarkedForDeletion).AsEnumerable().Where(c => c.CanView(user)).OrderBy(c => c.CompetitionName);

            foreach (Competition competition in query)
                yield return competition.GetSearchResultViewModel(user, searchType);
        }

        public static CompetitionSearchResultViewModel GetSearchResultViewModel(this Competition competition, IPrincipal user, SearchType searchType)
        {
            return new CompetitionSearchResultViewModel()
            {
                Competition = competition,
                IsVisible = competition.CanView(user),
                IsEditVisible = searchType == SearchType.Details ? competition.CanView(user) : false,
                IsDeleteVisible = (searchType == SearchType.Details || (searchType == SearchType.RecordForApproval && !competition.IsApproved)) ? competition.CanDelete(user) : false,
                IsViewApprovalsVisible = (searchType == SearchType.Details || searchType == SearchType.PendingApprovals) ? competition.CanApprove(user) : false,
                IsConfirmApprovalVisible = searchType == SearchType.RecordForApproval ? competition.CanApprove(user) : false
            };
        }

        public static CompetitionViewModel GetCompetitionViewModel(this Competition competition, IPrincipal user, SearchType searchType)
        {
            return new CompetitionViewModel()
            {
                CompetitionSearchResultViewModel = GetSearchResultViewModel(competition, user, searchType)
            };
        }

        public static void Edit(this Competition competition, CompetitionDataModel competitionDataModel, IPrincipal user)
        {
            competition.ApprovalGuid = competitionDataModel.ApprovalGuid;
            competition.CompetitionName = competitionDataModel.CompetitionName;
            competition.CompetitionType = competitionDataModel.CompetitionType;
            competition.LocationType = competitionDataModel.LocationType;
            competition.CountryGuid = competitionDataModel.CountryGuid;
            competition.ActiveFrom = competitionDataModel.ActiveFrom;
            competition.ActiveTo = competitionDataModel.ActiveTo;
            competition.IsApproved = competition.IsApproved ? user.IsApprover() : false;
            competition.AuditUserId = user.GetUserId();
            competition.AuditTimestamp = DateTime.Now;
        }

        public static void Approve(this Competition competition, Competition previousApproved)
        {
            previousApproved.CompetitionName = competition.CompetitionName;
            previousApproved.CompetitionType = competition.CompetitionType;
            previousApproved.LocationType = competition.LocationType;
            previousApproved.CountryGuid = competition.CountryGuid;
            previousApproved.ActiveFrom = competition.ActiveFrom;
            previousApproved.ActiveTo = competition.ActiveTo;
            previousApproved.AuditUserId = competition.AuditUserId;
            previousApproved.AuditTimestamp = competition.AuditTimestamp;
        }
    }
}